﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;


namespace monitor_server_2_WPF.MainWindowControls
{
    static class MainWindowExpander
    {
        //private static readonly Color ExpandersColor = Colors.LightBlue;
        //private static readonly Color NiceBlue = Color.FromArgb(0xFF, 0x00, 0x7A, 0xCC);
        private static readonly Brush ExpandersBackgroundBrush = new SolidColorBrush(Colors.Transparent);
        private static readonly Brush ExpandersListBackgroundBrush = new SolidColorBrush(Colors.Transparent);
        //private static readonly Color ExpandersHighlightedItemColor = Colors.Beige;
        //private static readonly Color ExpandersHighlightedItemColor = Colors.AliceBlue;
        private static readonly Color ExpandersHighlightedItemColor = Res.NiceBlue;//Color.FromRgb(0,122,204);
        private static readonly Brush ExpandersHighlightedItemBrush = new SolidColorBrush(ExpandersHighlightedItemColor);
        private static readonly Brush ExpandersBorderBrush = new SolidColorBrush(Colors.White);
        private static readonly Thickness NulThickness = new Thickness(0, 0, 0, 0);
        
        public static Expander Create(string caption, string imageResourceName)
        {
            var tlConfig = new Label { Content = caption, FontSize = 14, Foreground = new SolidColorBrush(Colors.White)};
            //image
            var imConfig = new Image { Source = Res.GetBitMapFromPng(imageResourceName), Width = 24, Height = 24 };
            //dockpanel
            var dpConfig = new DockPanel();
            dpConfig.Children.Add(imConfig);
            dpConfig.Children.Add(tlConfig);
            //init
            var expConfig = new Expander
            {
                ExpandDirection = ExpandDirection.Down,
                IsExpanded = false,
                Background = ExpandersBackgroundBrush,
                Header = dpConfig
            };

            //expConfig.Expanded += ExpConfigOnExpanded;

            //Content
            var lvConfig = new ListView
            {
                BorderThickness = NulThickness,
                Background = ExpandersListBackgroundBrush,
                SelectionMode = SelectionMode.Single,
                ItemContainerStyle = GetExpanderItemsStyle()
                
            };
            expConfig.Style = GetExpanderStyle();
            //expConfig.BorderThickness = new Thickness(0,0,0,0);
            expConfig.Content = lvConfig;
            expConfig.Expanded += categoryExpander_Expanded;
            expConfig.Collapsed += categoryExpander_Collapsed;
            return expConfig;
        }

        public static void AddItem(ref Expander exp, string caption, RoutedEventHandler handler = null)
        {

            var lvi = new ListViewItem();

            var t = new TextBlock();
            t.Inlines.Add(caption);
            t.Margin = new Thickness(30, 6, 0, 6);

            lvi.Content = t;
            //lvi.Foreground = new SolidColorBrush(Colors.White);
            lvi.BorderThickness = new Thickness(0, 0, 0, 0);

            if(handler != null)
                lvi.Selected += handler;

            (exp.Content as ListView).Items.Add(lvi);
        }

        public static void AddExpanderOnPanel(ref StackPanel sp, Expander exp)
        {
            //var bord = new Border();
            //bord.Background = new SolidColorBrush(Colors.Violet);
            //bord.CornerRadius = new CornerRadius(0, 0, 0, 0);
            //bord.Background = exp.Background;
            //exp.Margin = new Thickness(1,1,1,1);
            sp.Children.Add(exp);
        }

        private static Style GetExpanderItemsStyle()
        {
            var s = new Style(typeof(ListViewItem));

            s.Resources.Add(SystemColors.ControlBrushKey, ExpandersHighlightedItemBrush);
            s.Resources.Add(SystemColors.HighlightBrushKey, ExpandersHighlightedItemBrush);
            //s.Resources.Add(SystemColors.HighlightTextBrushKey, new SolidColorBrush(Colors.White));

            var t1 = new Trigger {Property = UIElement.IsMouseOverProperty, Value = true};

            var s1 = new Setter {Property = Control.BackgroundProperty, Value = Res.MouseOverBrush};
            t1.Setters.Add(s1);

            s.Triggers.Add(t1);

            var t2 = new Trigger
                {
                    Property = ListBoxItem.IsSelectedProperty, 
                    Value = true
                };

            var s2 = new Setter
                {
                    Property = Control.ForegroundProperty, 
                    //Value = new SolidColorBrush(Colors.Black)
                    Value = new SolidColorBrush(Colors.White)
                };

            t2.Setters.Add(s2);

            s.Triggers.Add(t2);

            var s3 = new Setter
                {
                    Property = Control.ForegroundProperty, 
                    Value = new SolidColorBrush(Colors.White)
                };

            s.Setters.Add(s3);

            var s4 = new Setter
                {
                    Property = FrameworkElement.FocusVisualStyleProperty, 
                    Value = null
                };

            s.Setters.Add(s4);

            return s;
        }

        //Настройка внешности экспандера
        private static Style GetExpanderStyle()
        {
            var s = new Style(typeof(Expander));
            var t = new Trigger
                {
                    Property = Expander.IsExpandedProperty, 
                    Value = true
                };

            //Рамка экспандера (толщина)
            var sbt = new Setter
                {
                    Property = Control.BorderThicknessProperty, 
                    Value = new Thickness(0, 0, 0, 0)
                };

            //Рамка экспандера (кисть)
            var sbb = new Setter
                {
                    Property = Control.BorderBrushProperty, 
                    Value = ExpandersBorderBrush
                };

            t.Setters.Add(sbt);
            t.Setters.Add(sbb);

            s.Triggers.Add(t);

            return s;
        }

        private static void categoryExpander_Expanded(object sender, RoutedEventArgs e)
        {
            var exp = (Expander) sender;
            //var panel = (StackPanel)((exp.Parent as Border).Parent);
            var panel = (StackPanel)((exp).Parent);
            foreach (var child in panel.Children)
            {
                if (child is Expander)
                {
                    if (child != sender)
                        (child as Expander).IsExpanded = false;
                }
            }
            
            //var a = new DoubleAnimation(exp.ActualHeight, 150,new Duration(new TimeSpan(0,0,0,0,150)));
            //exp.BeginAnimation(Expander.MaxHeightProperty, a);
        }

        private static void categoryExpander_Collapsed(object sender, RoutedEventArgs e)
        {
            var exp = (Expander)sender;
            foreach (ListViewItem item in (exp.Content as ListView).Items)
            {
                item.IsSelected = false;
            }
        }


    }
}
